
//
//  HPMainViewController.swift
//  MyWeibo
//
//  Created by apple on 16/4/6.
//  Copyright © 2016年 Apengpeng. All rights reserved.
//

import UIKit

class HPMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 添加nav 控制器
        addChildViewControllers()

        replaceSystemTabBar()
 
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
    }
    
    //MARK: plusBtn 点击方法实现
    @objc private func plusBtnDidClick() {
        
        let composedView = HPComposedView()
        print(composedView.frame)
        
        // 添加控制器
        composedView.show(self)
        
//        UIApplication.sharedApplication().keyWindow?.addSubview(composedView)
    }
    
    //MARK: 使用kvc替换系统的tabbar
    private func replaceSystemTabBar() {
        
        let MyTabBar = HPMainTabbar()
        // 给按钮添加方法
        MyTabBar.plusBtn.addTarget(self, action: "plusBtnDidClick", forControlEvents: .TouchUpInside)
        setValue(MyTabBar, forKey: "tabBar")
        MyTabBar.backgroundColor = UIColor.blueColor()
    }
    
    // MARK: 添加子控制器
    private func addChildViewControllers() {
        addChildViewControllers(HPHomeViewController(), title: "首页", norlImage: "tabbar_home", indx: 0)
        addChildViewControllers(HPMessageViewController(), title: "消息", norlImage: "tabbar_message_center", indx: 1)
        addChildViewControllers(HPDiscViewController(), title: "发现", norlImage: "tabbar_discover", indx: 2)
        addChildViewControllers(HPMeViewController(), title: "我", norlImage: "tabbar_profile", indx: 3)
    }

    //MARK: 设置子控制器
    private func addChildViewControllers(vc :UIViewController, title :String, norlImage :String, indx: Int) {
        
//        homeVc.view.backgroundColor = UIColor.greenColor()
//        vc.tabBarItem.title = title
//        vc.navigationItem.title = title
        
        //MARK: 以下两行代码的顺序很关键, 会导致tabBar上的文字不显示
        vc.tabBarItem = HPtabBarItem()
        vc.title = title
        
        vc.tabBarItem.tag = baseTag + indx
        vc.tabBarItem.image = UIImage(named: norlImage)
        vc.tabBarItem.selectedImage = UIImage(named: norlImage + "_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        // 设置文字不被渲染
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: .Selected)
        // 创建导航控制器
        let nav = HPBaseNavController(rootViewController: vc)
        // 添加到控制器
        addChildViewController(nav)
    }
    

    // 设置tabBar 上的图片动画
    override func tabBar(tabBar: UITabBar, didSelectItem item : UITabBarItem) {
        var indx = 0
        for subView in tabBar.subviews {
            
            if subView.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                if indx == item.tag - baseTag {
                    
                    for v in subView.subviews {
                        if v.isKindOfClass(NSClassFromString("UITabBarSwappableImageView")!) {
                            
                            UIView.animateWithDuration(0.5, animations: { () -> Void in
                                
                                // 执行动画
                                v.transform = CGAffineTransformMakeScale(0.7, 0.7)
                                
                                }, completion: { (_) -> Void in
                                    UIView.animateWithDuration(0.8, animations: { () -> Void in
                                        v.transform = CGAffineTransformMakeScale(1, 1)
                                    })
                            })
                        }
                    }
                }
                indx++
            }
        }
    }
}



